package com.rabbitmq.learn.work.fair;

import com.rabbitmq.client.*;
import com.rabbitmq.learn.common.ConnectionUtils;
import com.rabbitmq.learn.common.RabbitMqConstant;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by Lxk on 2019/8/11.
 */
public class Receiver2 {

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection con = ConnectionUtils.getConnection();

        Channel channel = con.createChannel();

        channel.queueDeclare(RabbitMqConstant.Work.QUEUE_NAME,false,false,false,null);

        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body,"utf-8");
                System.out.println("Receiver2: " + msg);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    System.out.println("Receiver2 done!!");
                }
            }
        };
        boolean autoAck = true;
        channel.basicConsume(RabbitMqConstant.Work.QUEUE_NAME,autoAck,consumer);
    }

}
